package com.ruoyi.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dyun.tool.mybatis.domain.PageDomain;
import com.dyun.tool.user.utils.UserUtil;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.setting.security.utils.UserUtils;
import com.ruoyi.project.system.entity.SysTempData;
import com.ruoyi.project.system.service.SysTempDataService;
import lombok.AllArgsConstructor;
import net.dreamlu.mica.core.result.R;
import net.dreamlu.mica.core.utils.$;
import net.dreamlu.mica.core.validation.CreateGroup;
import net.dreamlu.mica.core.validation.UpdateGroup;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 临时数据表(SysTempData) 控制层
 *
 * @author dyun
 */
@RestController
@RequestMapping("system/tempdata")
@AllArgsConstructor
public class SysTempDataController extends BaseController {

    private final SysTempDataService sysTempDataService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(SysTempData sysTempData, PageDomain pageDomain) {
        return success(sysTempDataService.list(new LambdaQueryWrapper<SysTempData>()
                .eq(SysTempData::getCreateBy, UserUtils.currentLoginUser().getSysUser().getUserName())
                .eq(SysTempData::getTableName, sysTempData.getTableName())));
    }

    /**
     * 分页
     */
    @GetMapping("/page")
    public R<IPage<SysTempData>> page(@ApiIgnore SysTempData sysTempData, PageDomain pageDomain) {
        IPage<SysTempData> pages = sysTempDataService.selectSysTempDataPage(startPage(pageDomain), sysTempData);
        return success(pages);
    }

    /**
     * 详情
     */
    @GetMapping("/detail")
    public R<SysTempData> detail(Long id) {
        return success(sysTempDataService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public R add(@Validated(value = {CreateGroup.class}) @RequestBody SysTempData sysTempData) {
        return status(sysTempDataService.saveIgnore(sysTempData));
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public R update(@Validated(value = {UpdateGroup.class}) @RequestBody SysTempData sysTempData) {
        return status(sysTempDataService.updateById(sysTempData));
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{ids}")
    public R delete(@PathVariable Long[] ids) {
        List<SysTempData> sysTempDatas = new ArrayList<>();
        List<Long> idList = new ArrayList<>(Arrays.asList(ids));
        idList.forEach(v -> {
            SysTempData sysTempData = new SysTempData();
            sysTempData.setId(v);
            sysTempDatas.add(sysTempData);
        });
        boolean result = sysTempDataService.updateBatchById(sysTempDatas) && sysTempDataService.removeByIds(idList);
        return status(result);
    }
}